package kr.hkit.powerjava.chapter14.submit;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class InterestCnt extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private InsertPanel inputText01;
	private InsertPanel inputText02;
	private ResultPanel outputText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InterestCnt frame = new InterestCnt();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InterestCnt() {
		setTitle("이자 계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 291, 181);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 1, 0, 0));
		
		panel = new JPanel();
		contentPane.add(panel);

		inputText01 = new InsertPanel("원금을 입력하시오");
		panel.add(inputText01);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);

		inputText02 = new InsertPanel("이율을 입력하시오");
		panel_1.add(inputText02);
		
		panel_2 = new JPanel();
		contentPane.add(panel_2);
		
		JButton btnTransfer = new JButton("변환");
		btnTransfer.addActionListener(this);
		panel_2.add(btnTransfer);
		
		panel_3 = new JPanel();
		contentPane.add(panel_3);
		
		outputText = new ResultPanel();
		panel_3.add(outputText);
		
	}

	public void actionPerformed(ActionEvent e) {
		int principal = 0;
		double interest = 0.0;
		if (e.getActionCommand().equals("변환")) {
			try {
				principal = Integer
						.parseInt(inputText01.getInsertText().trim());
			} catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능합니다.");
				inputText01.setInsertText("");
			}
			try {
				interest = Double.parseDouble(inputText02.getInsertText()
						.trim());
			} catch (NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능합니다.");
				inputText02.setInsertText("");

			}
			outputText.setResultText("이자는  연 " + 
			(int)(principal * (interest / 100)) + "만원 입니다.");
		}
	}
}
